package com.tang.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tang.pojo.Books;
import com.tang.service.BookService;
import com.tang.service.impl.BookServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author Tom
 * @ClassName BookController
 * @Date 2021/10/24 13:10
 */
@Controller
@RequestMapping("/admin")
public class BookController {
    @Autowired
    private BookService bookService;

    //查询所有书
    @RequestMapping("/queryAll")
    public String queryListBook(Model model, Integer currentPage, HttpServletRequest request) {
        PageHelper.startPage(currentPage,5);
        List<Books> books = bookService.queryAllBook();
        PageInfo page = new PageInfo(books);
        HttpSession session = request.getSession();
        session.setAttribute("page",page);
        model.addAttribute("list", books);
        return "allBook";
    }
//    @RequestMapping("/queryAllBook")
//    public String queryBooks(Model model) {
//        List<Books> books = bookService.queryAllBook();
//        model.addAttribute("list", books);
//        return "allBook";
//    }
    @RequestMapping("/toAddBook")
    public String addBook() {
        return "addBook";
    }

    //添加书籍后,重定向到首页
    @RequestMapping("/addSuccess")
    public String addSuccess(Books books,HttpServletRequest request) {
        System.out.println("成功添加" + books);
        bookService.addBook(books);
        HttpSession session = request.getSession();
        PageInfo page = (PageInfo) session.getAttribute("page");
        int currentPage = page.getPageNum();
        return "redirect:/admin/queryAll?currentPage="+currentPage;
    }
}
    